package com.bhs.sansong.adapter;

import android.content.Context;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.bean.BillBean;
import com.bhs.sansong.utils.DateUtils;

import java.util.List;

public class StickyExampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int FIRST_STICKY_VIEW = 1;
    public static final int HAS_STICKY_VIEW = 2;
    public static final int NONE_STICKY_VIEW = 3;

    private Context context;
    private List<BillBean> stickyExampleModels;

    public StickyExampleAdapter(Context context, List<BillBean> recyclerViewModels) {
        this.context = context;
        this.stickyExampleModels = recyclerViewModels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bill, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof RecyclerViewHolder) {
            RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) viewHolder;
            BillBean stickyExampleModel = stickyExampleModels.get(position);
            recyclerViewHolder.mTvBill.setText(stickyExampleModel.getLog_type_msg());
            recyclerViewHolder.mTvMoney.setText(Double.parseDouble(stickyExampleModel.getNumber()) > 0 ? String.format("+%s", stickyExampleModel.getNumber()) : stickyExampleModel.getNumber());
            recyclerViewHolder.mTvTime.setText(DateUtils.getTime2(stickyExampleModel.getCreate_time()));

            if (position == 0) {
                recyclerViewHolder.mTvDate.setVisibility(View.VISIBLE);
                recyclerViewHolder.mTvDate.setText(DateUtils.getYearMonth(stickyExampleModel.getCreate_time()));
                recyclerViewHolder.itemView.setTag(FIRST_STICKY_VIEW);
            } else {
                if (!TextUtils.equals(DateUtils.getYearMonth(stickyExampleModel.getCreate_time()), DateUtils.getYearMonth(stickyExampleModels.get(position - 1).getCreate_time()))) {
                    recyclerViewHolder.mTvDate.setVisibility(View.VISIBLE);
                    recyclerViewHolder.mTvDate.setText(DateUtils.getYearMonth(stickyExampleModel.getCreate_time()));
                    recyclerViewHolder.itemView.setTag(HAS_STICKY_VIEW);
                } else {
                    recyclerViewHolder.mTvDate.setVisibility(View.GONE);
                    recyclerViewHolder.itemView.setTag(NONE_STICKY_VIEW);
                }
            }
            recyclerViewHolder.itemView.setContentDescription(DateUtils.getYearMonth(stickyExampleModel.getCreate_time()));
        }
    }

    @Override
    public int getItemCount() {
        return stickyExampleModels == null ? 0 : stickyExampleModels.size();
    }


    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvDate;
        private TextView mTvBill;
        private Button mBtnTopSpeed;
        private Button mBtnAppoint;
        private Button mBtnMakeAppointment;
        private TextView mTvMoney;
        private TextView mTvTime;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mTvBill = itemView.findViewById(R.id.tv_bill);
            mBtnTopSpeed = itemView.findViewById(R.id.btn_top_speed);
            mBtnAppoint = itemView.findViewById(R.id.btn_appoint);
            mBtnMakeAppointment = itemView.findViewById(R.id.btn_make_appointment);
            mTvMoney = itemView.findViewById(R.id.tv_money);
            mTvTime = itemView.findViewById(R.id.tv_time);
        }
    }
}
